﻿// See https://aka.ms/new-console-template for more information


using System.Reflection;
using Microsoft.Extensions.DependencyInjection;

class Program
{


    static void Main(string[] args)
    {
        //原生反射  
        //ITank tank = new HeavyTank();
        //var t = tank.GetType();
        //object o = Activator.CreateInstance(t);
        //MethodInfo fireMi = t.GetMethod("Fire");
        //MethodInfo runMi = t.GetMethod("Run");
        //fireMi.Invoke(o, null);
        //runMi.Invoke(o, null);

        var sc=new ServiceCollection();
        //动态类型  实现类型
        sc.AddScoped(typeof(ITank), typeof(MediumTank));
        sc.AddScoped(typeof(IVehicle), typeof(MediumTank));
        sc.AddScoped(typeof(IVehicle), typeof(Car));
        sc.AddScoped<Driver>();
        var sp=sc.BuildServiceProvider();
        var driver=sp.GetService<Driver>();
        //=========分割线==========
        ITank itan=sp.GetService<ITank>();
        itan.Fire();
        driver.Drive();
    }
}

class Driver
{
    private IVehicle _vehicle;

    public Driver(IVehicle vehicle)
    {
        _vehicle = vehicle;
    }

    public void Drive()
    {
        _vehicle.Run();
    }
}

interface IVehicle
{
    void Run();
}

class Car : IVehicle
{
    public void Run()
    {
        Console.WriteLine("Car is running ...");
    }
}

class Truck : IVehicle
{
    public void Run()
    {
        Console.WriteLine("Truck is running ...");
    }
}

interface ITank
{
    void Fire();
    void Run();
}

class LightTank : ITank
{
    public void Fire()
    {
        Console.WriteLine("Boom!");
    }

    public void Run()
    {
        Console.WriteLine("Ka ka ka ...");
    }
}

class MediumTank : ITank
{
    public void Fire()
    {
        Console.WriteLine("Boom!!");
    }

    public void Run()
    {
        Console.WriteLine("Ka! ka! ka! ...");
    }
}

class HeavyTank : ITank
{
    public void Fire()
    {
        Console.WriteLine("Boom!!!");
    }

    public void Run()
    {
        Console.WriteLine("Ka!! ka!! ka!! ...");
    }
}